package com.example.mrzhou.srtfoodapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by SRTfood on 2017/1/12.
 */

public class FreshAdapter extends ArrayAdapter<Fresh>{

    //fresh类的列表适配器

    private int resourceId;

    public FreshAdapter(Context context, int textViewResourceId, List<Fresh> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        Fresh fresh=getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder=new ViewHolder();
            viewHolder.freshImage= (ImageView) view.findViewById(R.id.fresh_image);
            viewHolder.freshName= (TextView) view.findViewById(R.id.button_category);
            view.setTag(viewHolder);
        } else {
            view=convertView;
            viewHolder=(ViewHolder)view.getTag();
        }
        viewHolder.freshImage.setImageResource(fresh.getFreshImage());
        viewHolder.freshName.setText(fresh.getFreshName());
        return view;
    }

    //将对象储存于一个内部类中
    class ViewHolder{

        ImageView freshImage;

        TextView freshName;
    }
}
